
#include "main.h"
#include "drv832x.h"
#include "usr_uart.h"
#include "usr_flash.h"

static uint8_t usr_uart1_rx_buff[100] = {0x00};
static uint8_t usr_uart1_rx_buff_index = 0;


void usr_uart_cmd_handle(uint8_t *ptr, uint8_t len);




void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if(huart->Instance == USART1)
    {
        // 处理接收到的数据
        if(usr_uart1_rx_buff_index < sizeof(usr_uart1_rx_buff) - 1)
        {
            if( usr_uart1_rx_buff_index > 2                             &&
                usr_uart1_rx_buff[usr_uart1_rx_buff_index - 1] == '\r'  &&
                usr_uart1_rx_buff[usr_uart1_rx_buff_index - 0] == '\n'  )
            {
                usr_uart_cmd_handle(usr_uart1_rx_buff, usr_uart1_rx_buff_index);
                usr_uart1_rx_buff_index = 0;
            }
            else
            {
                usr_uart1_rx_buff_index++;
            }
            // 继续开启下一次接收中断
            HAL_UART_Receive_IT(&huart1, &usr_uart1_rx_buff[usr_uart1_rx_buff_index], 1);
        }
        else
        {
            // 缓冲区已满，可进行相应处理
            usr_uart1_rx_buff_index = 0;
            HAL_UART_Receive_IT(&huart1, &usr_uart1_rx_buff[usr_uart1_rx_buff_index], 1);
        }
    }
}

void usr_uart_init(void)
{
    // 开启串口接收中断
    HAL_UART_Receive_IT(&huart1, &usr_uart1_rx_buff[usr_uart1_rx_buff_index], 1);

}




void usr_uart_cmd_handle(uint8_t *ptr, uint8_t len)
{
    switch(ptr[0])
    {
        case 0x00:/*  DRV EN 开/关   */
        {
            drv832x_out_ctl(ptr[1]);
        }break;
        case 0x02:/*  启动/关闭 FOC   */
        {
            sys.foc_start = ptr[1];
        }break;
        case 0x03:/*  修改打印内容   */
        {
            sys.print_tag = ptr[1];
        }break;
        case 0x04:/*  读取DRV状态   */
        {
            printf("Fault:0x%X\r\n", drv832x_read_reg(0x00));
            printf("VGS:0x%X\r\n", drv832x_read_reg(0x01));
        }break;
        case 0x05:/*  重新学习   */
        {
            sys.foc_study = 1;
        }break;
        case 0x07:/*  设置uq   */
        {
            if(ptr[1] == 0)
            {
                sys.foc_controller.uq += 0.01f;
            }
            else
            {
                sys.foc_controller.uq -= 0.01f;
            }
        }break;
        case 0x08:/*  加减   */
        {
            if(ptr[1] == 0)
            {
                if(sys.foc_type == FOC_TYPE_POSITION)
                    sys.foc_controller.position_ref += 50;
                else
                    sys.foc_controller.speed_ref += 100;
            }
            else
            {
                if(sys.foc_type == FOC_TYPE_POSITION)
                    sys.foc_controller.position_ref -= 50;
                else
                    sys.foc_controller.speed_ref -= 100;
            }
        }break;
        case 0x09:/*  控制类型   */
        {
            if(ptr[1] == 0)
            {
                sys.foc_type = FOC_TYPE_POSITION;//位置闭环
            }
            else if(ptr[1] == 1)
            {
                sys.foc_type = FOC_TYPE_SPEED;//速度闭环
            }
            else if(ptr[1] == 2)
            {
                sys.foc_type = FOC_TYPE_CURRENT;//电流闭环
            }
            else if(ptr[1] == 3)
            {
                sys.foc_type = FOC_TYPE_KNOB;//旋钮
            }
        }break;
        case 0x0A:/*  设置编码器类型   */
        {
            if(ptr[1] == 0)
            {
                sys.cfg.senser_type = SENSER_TLE5012B_TYPE;
            }
            else if(ptr[1] == 1)
            {
                sys.cfg.senser_type = SENSER_AS5047P_TYPE;
            }
            else if(ptr[1] == 2)
            {
                sys.cfg.senser_type = SENSER_ABZ_TYPE;
            }
            if(save_cfg_to_flash(&sys.cfg) == FLASH_COMPLETE)
            {
                sys.fault_status.mild_fault.bit.flash_op_err = 0;
            }
            else
            {
                sys.fault_status.mild_fault.bit.flash_op_err = 1; 	
            }
        }break;
    }

}






